package gui;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
 * 主题管理器 - 统一管理界面风格
 */
public class ThemeManager {
    
    // 颜色方案
    public static final Color PRIMARY_COLOR = new Color(70, 130, 180); // 钢蓝色
    public static final Color SECONDARY_COLOR = new Color(240, 248, 255); // 爱丽丝蓝
    public static final Color ACCENT_COLOR = new Color(30, 144, 255); // 道奇蓝
    public static final Color TEXT_COLOR = new Color(33, 33, 33); // 深灰色
    public static final Color BACKGROUND_COLOR = new Color(245, 245, 245); // 淡灰色
    public static final Color SUCCESS_COLOR = new Color(46, 204, 113); // 绿色
    public static final Color WARNING_COLOR = new Color(241, 196, 15); // 黄色
    public static final Color DANGER_COLOR = new Color(231, 76, 60); // 红色
    
    // 字体
    public static final Font TITLE_FONT = new Font("微软雅黑", Font.BOLD, 18);
    public static final Font SUBTITLE_FONT = new Font("微软雅黑", Font.BOLD, 16);
    public static final Font NORMAL_FONT = new Font("微软雅黑", Font.PLAIN, 14);
    public static final Font SMALL_FONT = new Font("微软雅黑", Font.PLAIN, 12);
    public static final Font BOLD_FONT = new Font("微软雅黑", Font.BOLD, 14);
    
    // 边框
    public static Border createTitleBorder(String title) {
        return BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(PRIMARY_COLOR, 1),
                title,
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                javax.swing.border.TitledBorder.DEFAULT_POSITION,
                SUBTITLE_FONT,
                PRIMARY_COLOR
        );
    }
    
    /**
     * 创建美化按钮
     */
    public static JButton createButton(String text) {
        JButton button = new JButton(text);
        button.setBackground(PRIMARY_COLOR);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setFont(BOLD_FONT);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        return button;
    }
    
    /**
     * 创建成功按钮
     */
    public static JButton createSuccessButton(String text) {
        JButton button = new JButton(text);
        button.setBackground(SUCCESS_COLOR);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setFont(BOLD_FONT);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        return button;
    }
    
    /**
     * 创建警告按钮
     */
    public static JButton createWarningButton(String text) {
        JButton button = new JButton(text);
        button.setBackground(WARNING_COLOR);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setFont(BOLD_FONT);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        return button;
    }
    
    /**
     * 创建危险按钮
     */
    public static JButton createDangerButton(String text) {
        JButton button = new JButton(text);
        button.setBackground(DANGER_COLOR);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setFont(BOLD_FONT);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        return button;
    }
    
    /**
     * 应用主题到组件
     */
    public static void applyTheme(JComponent component) {
        component.setFont(NORMAL_FONT);
        component.setForeground(TEXT_COLOR);
        component.setBackground(BACKGROUND_COLOR);
        
        if (component instanceof JButton) {
            JButton button = (JButton) component;
            if (button.getBackground() == SUCCESS_COLOR || 
                button.getBackground() == WARNING_COLOR || 
                button.getBackground() == DANGER_COLOR) {
                // 保持特殊按钮的颜色
            } else {
                button.setBackground(PRIMARY_COLOR);
            }
            button.setForeground(Color.WHITE);
            button.setFocusPainted(false);
            button.setBorderPainted(false);
            button.setFont(NORMAL_FONT);
        } else if (component instanceof JPanel) {
            component.setBackground(BACKGROUND_COLOR);
        } else if (component instanceof JLabel) {
            component.setForeground(TEXT_COLOR);
        } else if (component instanceof JTextField || component instanceof JTextArea) {
            component.setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createLineBorder(PRIMARY_COLOR),
                    BorderFactory.createEmptyBorder(5, 5, 5, 5)
            ));
        } else if (component instanceof JTable) {
            JTable table = (JTable) component;
            table.setGridColor(PRIMARY_COLOR.brighter());
            table.setSelectionBackground(ACCENT_COLOR);
            table.setSelectionForeground(Color.WHITE);
            table.setFont(NORMAL_FONT);
            table.getTableHeader().setFont(SUBTITLE_FONT);
            table.getTableHeader().setBackground(PRIMARY_COLOR);
            table.getTableHeader().setForeground(Color.WHITE);
        }
    }
    
    /**
     * 应用主题到整个容器及其子组件
     */
    public static void applyThemeRecursively(Container container) {
        for (Component component : container.getComponents()) {
            if (component instanceof JComponent) {
                applyTheme((JComponent) component);
            }
            
            if (component instanceof Container) {
                applyThemeRecursively((Container) component);
            }
        }
    }
}